package com.chuantou.yuba.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import com.chuantou.yuba.R;
import com.chuantou.yuba.util.FragmentUtil;

/**
 * 好友列表fragment
 * 
 * @author dl1001
 * 
 */
public class FriendFragment extends BaseFragment implements OnClickListener {
	private View mContentView;
	private View mNaviFollowsView;
	private View mNaviFansView;
	private View[] mNaviViews = new View[2];

	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		if (mContentView == null) {
			mContentView = inflater.inflate(R.layout.fg_friend, null);
			mNaviFollowsView = mContentView.findViewById(R.id.follows_button);
			mNaviFollowsView.setOnClickListener(this);
			mNaviViews[0] = mNaviFollowsView;
			mNaviFansView = mContentView.findViewById(R.id.fans_button);
			mNaviFansView.setOnClickListener(this);
			mNaviViews[1] = mNaviFansView;
			mNaviFollowsView.performClick();
		}
		ViewGroup parentView = (ViewGroup) mContentView.getParent();
		if (parentView != null) {
			parentView.removeView(mContentView);
		}
		return mContentView;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.follows_button:
		case R.id.fans_button:
			switchContentView(v.getId());
			break;

		default:
			break;
		}
	}

	private void switchContentView(int id) {
		String tag = String.valueOf(id);
		FragmentManager fragmentManager = getChildFragmentManager();
		FragmentUtil.hideAllFragment(fragmentManager);
		Fragment fragment = fragmentManager.findFragmentByTag(tag);
		FragmentTransaction transaction = fragmentManager.beginTransaction();
		if (fragment == null) {
			switch (id) {
			case R.id.follows_button:
				fragment = new FollowsFragment();
				break;
			case R.id.fans_button:
				fragment = new FansFragment();
				break;
			default:
				fragment = new FollowsFragment();
				break;
			}
			transaction.add(R.id.friend_content, fragment, tag);
		} else {
			transaction.show(fragment);
		}
		transaction.commitAllowingStateLoss();
		for (View view : mNaviViews) {
			if (view.getId() == id) {
				view.setSelected(true);
			} else {
				view.setSelected(false);
			}
		}
	}
}
